package com.yinhe.module.gs.product.controller;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhProduct;
import com.yinhe.system.service.IYhProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 产品零件Controller
 *
 * @author yinhe
 * @date 2022-09-26
 */
@RestController
@RequestMapping("/gs/product")
public class YhProductsController extends BaseController
{
    @Autowired
    private IYhProductService yhProductService;

    /**
     * 查询产品零件列表
     */
    @PreAuthorize("@ss.hasPermi('gs:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhProduct yhProduct)
    {
        startPage();
        List<YhProduct> list = yhProductService.selectYhProductList(yhProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @PreAuthorize("@ss.hasPermi('gs:product:export')")
    @Log(title = "产品零件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhProduct yhProduct)
    {
        List<YhProduct> list = yhProductService.selectYhProductList(yhProduct);
        ExcelUtil<YhProduct> util = new ExcelUtil<YhProduct>(YhProduct.class);
        util.hideColumn("partName","partType","applicableModel","drawingNum","partNumber","productQuantity","productLegend",
                "orderQuantity","measurementUnit","differentiateStates","userId","deptId",
                "note3","note4","note5","note6","note7","note8","note9","note10"
                );
     /*   util.hideColumn("partType");
        util.hideColumn("applicableModel");
        util.hideColumn("drawingNum");
        util.hideColumn("partNumber");
        util.hideColumn("orderQuantity");
        util.hideColumn("measurementUnit");
        util.hideColumn("differentiateStates");
        util.hideColumn("userId");
        util.hideColumn("deptId");
        util.hideColumn("note3");
        util.hideColumn("note4");
        util.hideColumn("note5");
        util.hideColumn("note6");
        util.hideColumn("note7");
        util.hideColumn("note8");
        util.hideColumn("note9");
        util.hideColumn("note10");*/






        util.exportExcel(response, list, "产品数据");
    }






    /**
     * 导出零件列表
     */
    @PreAuthorize("@ss.hasPermi('gs:product:export')")
    @Log(title = "零件", businessType = BusinessType.EXPORT)
    @PostMapping("/export1")
    public void export1(HttpServletResponse response, YhProduct yhProduct)
    {
        List<YhProduct> list = yhProductService.selectYhProductList(yhProduct);
        ExcelUtil<YhProduct> util = new ExcelUtil<YhProduct>(YhProduct.class);
        util.hideColumn("productName","productType","productModel","productCode","productLegend",
                "productQuantity","differentiateStates","userId","deptId","orderQuantity",
                "note3","note4","note5","note6","note7","note8","note9","note10"
                );
       /* util.hideColumn("productType");
        util.hideColumn("productModel");
        util.hideColumn("productCode");
        util.hideColumn("productLegend");
        util.hideColumn("productQuantity");
       // util.hideColumn("measurementUnit");
        util.hideColumn("differentiateStates");
        util.hideColumn("userId");
        util.hideColumn("deptId");
        util.hideColumn("note3");
        util.hideColumn("note4");
        util.hideColumn("note5");
        util.hideColumn("note6");
        util.hideColumn("note7");
        util.hideColumn("note8");
        util.hideColumn("note9");
        util.hideColumn("note10");
*/





        util.exportExcel(response, list, "零件数据");
    }








    /**
     * 获取产品零件详细信息
     */
    //@PreAuthorize("@ss.hasPermi('gs:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {





        return AjaxResult.success(yhProductService.selectYhProductById(id));
    }

    /**
     * 新增产品零件
     */
    @PreAuthorize("@ss.hasPermi('gs:product:add')")
    @Log(title = "产品零件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhProduct yhProduct)
    {
        YhProduct Y = new YhProduct();
       String qian = yhProduct.getNote1()+yhProduct.getNote2();
        //System.out.println(qian+"qian");

        //System.out.println( yhProductService.selectYhProductList(Y).size()+"size");


        if(Integer.valueOf(yhProduct.getDifferentiateStates())==0){
            String str = String.format("%04d", yhProductService.selectYhProductList(Y).size());
            String code = qian+str;
            yhProduct.setProductCode(code);
        }else {
            String str = String.format("%05d", yhProductService.selectYhProductList(Y).size());
            String code = qian+"0000"+str;
            yhProduct.setPartNumber(code);
        }




        return toAjax(yhProductService.insertYhProduct(yhProduct));
    }

    /**
     * 修改产品零件
     */
    @PreAuthorize("@ss.hasPermi('gs:product:edit')")
    @Log(title = "产品零件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhProduct yhProduct)
    {
        return toAjax(yhProductService.updateYhProduct(yhProduct));
    }

    /**
     * 删除产品零件
     */
    @PreAuthorize("@ss.hasPermi('gs:product:remove')")
    @Log(title = "产品零件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhProductService.deleteYhProductByIds(ids));
    }

    /**
     * 获取产品类型返查询产品列表
     */
    @RequestMapping("product/{type}")
    public TableDataInfo list(@PathVariable("type") String type){
        List<YhProduct> result = new ArrayList<YhProduct>();
        String[] split = type.split(",");
        for (int i = 0; i < split.length; i++) {
            List<YhProduct> product = yhProductService.selectProductByType(split[i]);
           for (YhProduct a:product){
               result.add(a);
           }
        }
        return getDataTable(result);

    }
}
